import axios from 'axios'
import Vue from 'vue'
import router from 'router'

const http = axios.create({
    baseURL: process.env.BASE_API
})

http.interceptors.request.use(function(config){
    console.log(config);
    if(localStorage.token){
        config.headers.Authorization = 'Bearer' + localStorage.token
    }
    return config
},function(error){
    return Promise.reject(error)
})
http.interceptors.response.use(res => {
  console.log(res);
    return res
  }, err => {
    console.log(err.response.data);
    if (err.response.data.message) {
      Vue.prototype.$message({
        type: 'error',
        message: err.response.data.message
      })
      
      if (err.response.status === 401) {
        router.push('/login')
      }
    }
    
    return Promise.reject(err)
  })

export default http;